package com.gbei.center.oauth.controller;

import com.gbei.center.base.BaseController;
import com.gbei.center.config.restauth.RestAuthConfig;
import com.gbei.center.oauth.req.AgreeReq;
import com.gbei.center.oauth.req.LoginReq;
import com.gbei.center.oauth.req.RefreshTokenReq;
import com.gbei.center.oauth.req.TokenReq;
import com.gbei.center.oauth.service.AuthorizationService;
import com.gbei.center.utils.message.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 基于oauth2.0相关的授权相关操作
 *
 */
@RestController
@RequestMapping("oauth2")
@Api(description = "授权相关接口")
public class OauthController extends BaseController{


    @Autowired
    private AuthorizationService authorizationService;


    @RestAuthConfig(release = true)
    @ApiOperation(value = "授权登录验证获取授权码code")
    @PostMapping("checkLogin")
    public Result<?> checkLogin(@Valid @RequestBody LoginReq req, BindingResult br,HttpServletRequest request){
        checkParams(br);
        return authorizationService.checkLogin(req,request);
    }

    @RestAuthConfig(release = true)
    @ApiOperation(value = "同意授权")
    @PostMapping("agree")
    public Result<?> agree(@Valid @RequestBody AgreeReq req,BindingResult br){
        checkParams(br);
        return authorizationService.agree(req);
    }



    @RestAuthConfig(release = true)
    @ApiOperation(value = "获取Token")
    @PostMapping("token")
    public Result<?> token(@Valid @RequestBody TokenReq req, BindingResult br){
        checkParams(br);
       return authorizationService.token(req);
    }

    @RestAuthConfig(release = true)
    @ApiOperation(value = "刷新Token")
    @PostMapping("refreshToken")
    public Result<?> refreshToken(@Valid @RequestBody RefreshTokenReq req, BindingResult br){
        checkParams(br);
        return authorizationService.refreshToken(req);
    }


}
